<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>aligned_storage</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.TypeTraits">
<link rel="up" href="../reference.html" title="Alphabetical Reference">
<link rel="prev" href="add_volatile.html" title="add_volatile">
<link rel="next" href="alignment_of.html" title="alignment_of">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="add_volatile.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="alignment_of.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typetraits.reference.aligned_storage"></a><a class="link" href="aligned_storage.html" title="aligned_storage">aligned_storage</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">Size</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">Align</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">aligned_storage</span>
<span class="special">{</span>
   <span class="keyword">typedef</span> <em class="replaceable"><code>see-below</code></em> <span class="identifier">type</span><span class="special">;</span>
<span class="special">};</span>
</pre>
<p>
        <span class="bold"><strong>type:</strong></span> a built-in or POD type with size
        <code class="computeroutput"><span class="identifier">Size</span></code> and an alignment that
        is a multiple of <code class="computeroutput"><span class="identifier">Align</span></code>.
      </p>
<p>
        <span class="bold"><strong>Header:</strong></span> <code class="computeroutput"> <span class="preprocessor">#include</span>
        <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_traits</span><span class="special">/</span><span class="identifier">aligned_storage</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
        or <code class="computeroutput"> <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_traits</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
      </p>
<p>
        On the GCC and Visual C++ compilers (or compilers that are compatible with
        them), we support requests for types with alignments greater than any built
        in type (up to 128-bit alignment). Visual C++ users should note that such
        "extended" types can not be passed down the stack as by-value function
        arguments.
      </p>
<div class="important"><table border="0" summary="Important">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../../doc/src/images/important.png"></td>
<th align="left">Important</th>
</tr>
<tr><td align="left" valign="top">
<p>
          Visual C++ users should be aware that MSVC has an elastic definition of
          alignment, for example consider the following code:
        </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">aligned_storage</span><span class="special">&lt;</span><span class="number">8</span><span class="special">,</span><span class="number">8</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">align_t</span><span class="special">;</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">alignment_of</span><span class="special">&lt;</span><span class="identifier">align_t</span><span class="special">&gt;::</span><span class="identifier">value</span> <span class="special">%</span> <span class="number">8</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
<span class="identifier">align_t</span> <span class="identifier">a</span><span class="special">;</span>
<span class="identifier">assert</span><span class="special">(((</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">uintptr_t</span><span class="special">)&amp;</span><span class="identifier">a</span> <span class="special">%</span> <span class="number">8</span><span class="special">)</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
<span class="keyword">char</span> <span class="identifier">c</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
<span class="identifier">align_t</span> <span class="identifier">a1</span><span class="special">;</span>
<span class="identifier">assert</span><span class="special">(((</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">uintptr_t</span><span class="special">)&amp;</span><span class="identifier">a1</span> <span class="special">%</span> <span class="number">8</span><span class="special">)</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
</pre>
<p>
        </p>
<p>
          In this code the final assert will fail for a 32-bit build because variable
          <code class="computeroutput"><span class="identifier">a1</span></code> is not aligned on an
          8-byte boundary. Had we used the MSVC intrinsic <code class="computeroutput"><span class="identifier">__alignof</span></code>
          in place of <code class="computeroutput"><span class="identifier">alignment_of</span></code>
          or <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">aligned_storage</span></code> in place of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">aligned_storage</span></code> the result would have
          been no different. In MSVC alignment requirements/promises only really
          apply to variables on the heap, not on the stack.
        </p>
<p>
          Further, although MSVC has a mechanism for generating new types with arbitrary
          alignment requirements, such types cannot be passed as function arguments
          on the program stack. Therefore had <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">aligned_storage</span><span class="special">&lt;</span><span class="number">8</span><span class="special">,</span><span class="number">8</span><span class="special">&gt;::</span><span class="identifier">type</span></code>
          been a type declared with <code class="computeroutput"><span class="identifier">__declspec</span><span class="special">(</span><span class="identifier">align</span><span class="special">(</span><span class="number">8</span><span class="special">))</span></code>
          we would break a great deal of existing code that relies on being able
          to pass such types through the program stack.
        </p>
</td></tr>
</table></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2000, 2011 Adobe Systems Inc, David Abrahams,
      Frederic Bron, Steve Cleary, Beman Dawes, Glen Fernandes, Aleksey Gurtovoy,
      Howard Hinnant, Jesse Jones, Mat Marcus, Itay Maman, John Maddock, Alexander
      Nasonov, Thorsten Ottosen, Roman Perepelitsa, Robert Ramey, Jeremy Siek, Robert
      Stewart and Steven Watanabe<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="add_volatile.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="alignment_of.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
